package com.seari.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.seari.bean.MenuPrivilege;
import com.seari.pojo.DataGridBean;
import com.seari.pojo.MessageBean;
import com.seari.pojo.QueryMenuPrivilege;
import com.seari.service.PrivilegeService;
import com.seari.utils.StringHelper;

/**
 * @author TOUC 按钮管理
 */
@Controller
@RequestMapping("privilege/")
public class PrivilegeController {
	
	@Autowired
	private	PrivilegeService privilegeService;
	
	

	@RequestMapping("privilegeSearch")
	@ResponseBody
	public DataGridBean<MenuPrivilege> privilegeSearch(HttpServletRequest req) {
		DataGridBean<MenuPrivilege> grid = new DataGridBean<MenuPrivilege>();
		String match = req.getParameter("match");
		String currPage = req.getParameter("page");
		String pageSize = req.getParameter("limit");

		QueryMenuPrivilege qu = new QueryMenuPrivilege();
		if (StringUtils.isNotBlank(currPage)
				&& StringUtils.isNotBlank(pageSize)) {
			qu.setStart((Integer.parseInt(currPage) - 1)
					* Integer.parseInt(pageSize));
			qu.setLimit(Integer.parseInt(pageSize));
		}
		List<MenuPrivilege> privileges = null;
		if (StringUtils.isNotBlank(match)) {
			qu.setMatch(match);
			privileges = privilegeService.privilegeSearch(qu);
			grid.setTotal(privileges.size());
			grid.setRows(privileges);
		}
		return grid;
	}
	
    /**
     * @param req
     * @return
     * 
     */
    @RequestMapping("getPrivilegeByMpid")
    @ResponseBody
    public DataGridBean<MenuPrivilege> getPrivilegeByMpid(HttpServletRequest req)
    {
    	String menuId = req.getParameter("mpid");
    	DataGridBean<MenuPrivilege> grid = new DataGridBean<MenuPrivilege>();
    	if(StringUtils.isNotBlank(menuId))
    	{
        List<MenuPrivilege> menuPrivileges = privilegeService.getPrivilegeByMpid(Integer.parseInt(menuId));
        grid.setRows(menuPrivileges);
        grid.setTotal(menuPrivileges.size());
    	}
    	return grid;
    }
    
    /**
     * @param req
     * @return
     * 
     */
    @RequestMapping("getPrivileges")
    @ResponseBody
    public DataGridBean<MenuPrivilege> getPrivileges(HttpServletRequest req)
    {
        DataGridBean<MenuPrivilege> grid = new DataGridBean<MenuPrivilege>();
        String currPage = req.getParameter("page");
        String pageSize = req.getParameter("limit");
		String match = req.getParameter("match");
        QueryMenuPrivilege qmp = new QueryMenuPrivilege();
        Integer count = 0;
        
        if(StringUtils.isNotBlank(currPage) && StringUtils.isNotBlank(pageSize))
        {
        	qmp.setStart((Integer.parseInt(currPage) - 1) * Integer.parseInt(pageSize));
        	qmp.setLimit(Integer.parseInt(pageSize));

        }
		List<MenuPrivilege> privileges = null;
		if (StringUtils.isNotBlank(match)) {
			qmp.setMatch(match);
			privileges = privilegeService.privilegeSearch(qmp);
			count = privilegeService.privilegeSearchCount(qmp);
			grid.setTotal(count);
			grid.setRows(privileges);
			return grid;
		}
        
         privileges = privilegeService.getPrivileges(qmp);
         count = privilegeService.getPrivilegesCount(qmp);
        grid.setRows(privileges);
        grid.setTotal(count);
    	 return grid;
    }
    
    @RequestMapping("updatePrivilege")
    @ResponseBody
    public MessageBean updatePrivilege(HttpServletRequest req)
    {
        MessageBean mb = new MessageBean();
       // String parameter = req.getParameter("upid");
        String mpid = req.getParameter("mpid");
        String pmName = req.getParameter("pmName");
        String pmCode = req.getParameter("pmCode");
        String pmComment = req.getParameter("pmComment");
        String parentId = req.getParameter("parentId");
        String statu = req.getParameter("statu");
        
        if(StringUtils.isBlank(mpid) 
        	|| StringUtils.isBlank(pmName) 
        	|| StringUtils.isBlank(pmCode)
        	|| StringUtils.isBlank(pmComment) 
        	|| StringUtils.isBlank(parentId))
        {
            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            mb.setMessage("表单信息不完整");
            return mb;
        }else{
            MenuPrivilege menuPrivilege = new MenuPrivilege();
        	 if(!StringUtils.isBlank(statu)){
        		 menuPrivilege.setStatu(statu);
        	 }
            QueryMenuPrivilege qup = new QueryMenuPrivilege();
            qup.setPmName(pmName);
            qup.setMpid(Integer.parseInt(mpid));
            int resultNum = privilegeService.checkMenuPrivilegeExist(qup);
            if(resultNum > 0 )
            {
                mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
                mb.setMessage("权限名重复");
                return mb;
            }
        	
            menuPrivilege.setMpid(Integer.parseInt(mpid));
            menuPrivilege.setPmName(pmName);
            menuPrivilege.setPmCode(pmCode);
            menuPrivilege.setPmComment(pmComment);
            menuPrivilege.setParentId(Integer.parseInt(parentId));
            
            int resultNum1 = privilegeService.updatePrivilege(menuPrivilege);
            if(resultNum1 < 0)
            {
                mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
                mb.setMessage("修改失败");
            }else{
                mb.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
                mb.setMessage("修改成功");
            }
        }
        return mb;
    }
    
    
    @RequestMapping("addPrivilege")
    @ResponseBody
    public MessageBean addPrivilege(HttpServletRequest req)
    {
        MessageBean mb = new MessageBean();
        String pmName = req.getParameter("pmName");
        String pmCode = req.getParameter("pmCode");
        String pmComment = req.getParameter("pmComment");
        String parentId = req.getParameter("parentId");
        String statu = req.getParameter("statu");
        
        if( StringUtils.isBlank(pmName) 
            || StringUtils.isBlank(pmCode)
            || StringUtils.isBlank(pmComment) 
            || StringUtils.isBlank(parentId))
        {
            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            mb.setMessage("表单信息不完整");
            return mb;
        }else{
        	
        	 MenuPrivilege menuPrivilege = new MenuPrivilege();
        	if(!StringUtils.isBlank(statu)){
                QueryMenuPrivilege qup = new QueryMenuPrivilege();
                menuPrivilege.setStatu(statu);
                qup.setPmName(pmName);
                int resultNum = privilegeService.checkMenuPrivilegeExist(qup);
                if(resultNum > 0 )
                {
                    mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
                    mb.setMessage("地址名重复");
                    return mb;
                }
        		
        	}
           
            menuPrivilege.setPmName(pmName);
            menuPrivilege.setPmCode(pmCode);
            menuPrivilege.setPmComment(pmComment);
            menuPrivilege.setParentId(Integer.parseInt(parentId));
            
            int resultNum1 = privilegeService.addPrivilege(menuPrivilege);
            if(resultNum1 < 0)
            {
                mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
                mb.setMessage("菜单权限添加失败");
            }else{
                mb.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
                mb.setMessage("菜单权限添加成功");
            }
        }
        return mb;
    }
    
    @RequestMapping("deletePrivilege")
    @ResponseBody
    public MessageBean deletePrivilege(HttpServletRequest req)
    {
        MessageBean mb = new MessageBean();
        String mpid = req.getParameter("mpid");
        if(StringUtils.isBlank(mpid))
        {
            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            mb.setMessage("表单信息不完整");
            return mb;
        }else
        {  
            int resultNum1 = privilegeService.deletePrivilege(Integer.parseInt(mpid));
            if(resultNum1 < 0)
            {
                mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
                mb.setMessage("删除失败");
            }else{
                mb.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
                mb.setMessage("删除成功");
            }
        }
        return mb;
    }
    
    
    
    

}
